package com.ruoyi.student.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.student.domain.DTO.TUnitInfoDTO;
import com.ruoyi.student.domain.TCourseInfo;
import com.ruoyi.student.domain.vo.TUnitInfoVo;
import com.ruoyi.student.service.ITCourseInfoService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.student.domain.TUnitInfo;
import com.ruoyi.student.service.ITUnitInfoService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 单元Controller
 * 
 * @author ruoyi
 * @date 2023-08-15
 */
@RestController
@RequestMapping("/student/unit")
public class TUnitInfoController extends BaseController
{
    @Autowired
    private ITUnitInfoService tUnitInfoService;

    @Autowired
    private ITCourseInfoService itCourseInfoService;

    /**
     * 查询单元列表
     */
    @PreAuthorize("@ss.hasPermi('student:unit:list')")
    @GetMapping("/list")
    public TableDataInfo list(TUnitInfo tUnitInfo)
    {
        startPage();
//        List<TUnitInfo> list = tUnitInfoService.selectTUnitInfoList(tUnitInfo);
        List<TUnitInfoVo> list = tUnitInfoService.selectTUnitInfoVoList(tUnitInfo);
        return getDataTable(list);
    }

    /**
     * 查询单元完成记录详细
     */
    @PreAuthorize("@ss.hasPermi('student:unit:listRecord')")
    @GetMapping("/listRecord")
    public TableDataInfo list(TUnitInfoDTO tUnitInfoDTO)
    {
        startPage();
//        List<TUnitInfo> list = tUnitInfoService.selectTUnitInfoList(tUnitInfo);
        List<TUnitInfoVo> list = tUnitInfoService.selectTUnitInfoVoRecordList(tUnitInfoDTO);
        return getDataTable(list);
    }

    /**
     * 导出单元列表
     */
    @PreAuthorize("@ss.hasPermi('student:unit:export')")
    @Log(title = "单元", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TUnitInfo tUnitInfo)
    {
        List<TUnitInfo> list = tUnitInfoService.selectTUnitInfoList(tUnitInfo);
        ExcelUtil<TUnitInfo> util = new ExcelUtil<TUnitInfo>(TUnitInfo.class);
        util.exportExcel(response, list, "单元数据");
    }

    /**
     * 获取单元详细信息
     */
    @PreAuthorize("@ss.hasPermi('student:unit:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {

//        return success(tUnitInfoService.selectTUnitInfoById(id));
        return success(tUnitInfoService.selectTUnitInfoVoById(id));
    }

    /**
     * 新增单元
     */
    @PreAuthorize("@ss.hasPermi('student:unit:add')")
    @Log(title = "单元", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TUnitInfo tUnitInfo)
    {
        SysUser user = SecurityUtils.getLoginUser().getUser();
        tUnitInfo.setCreatorId(user.getUserId());
        TCourseInfo tCourseInfo = itCourseInfoService.selectTCourseInfoById(Long.valueOf(tUnitInfo.getCourseId()));
        tUnitInfo.setCourseName(tCourseInfo.getName());
        return toAjax(tUnitInfoService.insertTUnitInfo(tUnitInfo));
    }

    /**
     * 修改单元
     */
    @PreAuthorize("@ss.hasPermi('student:unit:edit')")
    @Log(title = "单元", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TUnitInfo tUnitInfo)
    {
        TCourseInfo tCourseInfo = itCourseInfoService.selectTCourseInfoById(Long.valueOf(tUnitInfo.getCourseId()));
        tUnitInfo.setCourseName(tCourseInfo.getName());
        return toAjax(tUnitInfoService.updateTUnitInfo(tUnitInfo));
    }

    /**
     * 删除单元
     */
    @PreAuthorize("@ss.hasPermi('student:unit:remove')")
    @Log(title = "单元", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(tUnitInfoService.deleteTUnitInfoByIds(ids));
    }
}
